package tpi.gui.panels.caso

import dominio.ubicaciones.Pais
import org.apache.wicket.markup.html.form.DropDownChoice
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.panel.Panel
import org.apache.wicket.model.CompoundPropertyModel
import org.apache.wicket.model.PropertyModel
import org.uqbar.commons.model.UserException
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import tpi.gui.model.Investigacion
import tpi.gui.pages.caso.ResolverCasoPage
import org.apache.wicket.behavior.AttributeAppender

class AeropuertoPanel extends Panel {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Investigacion investigacion	
	private Pais destinoSeleccionado
		
	new(String id, Investigacion investigacion) {
		super(id)
		this.investigacion = investigacion
		this.defaultModel = investigacion.asCompoundModel
		
		val Form<Investigacion> form = new Form<Investigacion>("aeropuertoForm", new CompoundPropertyModel(this.investigacion))
		form.addChild(new DropDownChoice("destinos", 
			new PropertyModel(this, "destinoSeleccionado"), new PropertyModel(this.investigacion,"paisActual.conexiones")
		))
		
		form.addChild(new XButton("viajar").onClick = [| viajarADestinoSeleccionado()])
		val retroceder = new XButton("retroceder").onClick = [| retroceder()]
		if (this.investigacion.paisAnterior == null) retroceder.add(new AttributeAppender("disabled",true))
		form.addChild(retroceder)
		addChild(form)
	}
	
	def viajarADestinoSeleccionado() {
		if(destinoSeleccionado == null)
			throw new UserException("Debe elejir un destino antes de viajar :S")
		investigacion.viajar(destinoSeleccionado)
		destinoSeleccionado = null
		responsePage = new ResolverCasoPage(this.investigacion)
	}
	
	def retroceder() {
		this.investigacion.retroceder()
		responsePage = new ResolverCasoPage(this.investigacion)
	}
	
}